﻿using DevExpress.Xpo.Logger;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using GeneralDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZMotion;

namespace TableModule
{
    public partial class FormDebug : DevExpress.XtraEditors.XtraForm
    {
        List<AxisStatus> axisStatuses;
        public FormDebug()
        {
            InitializeComponent();
        }

        private void FormDebug_Load(object sender, EventArgs e)
        {
            comboBoxEditMode.SelectedIndex = 0;
            comboBoxEditDistance.SelectedIndex = 0;

            UpDateComBox();

            axisStatuses =new List<AxisStatus>();
            axisStatuses.Add(new AxisStatus("X"));
            axisStatuses.Add(new AxisStatus("Y"));
            axisStatuses.Add(new AxisStatus("Z"));
            axisStatuses.Add(new AxisStatus("U"));
            axisStatuses.Add(new AxisStatus("Conveyor"));
            gridControlTableStatus.DataSource = axisStatuses;

            DevExpress.XtraGrid.Views.Grid.GridView gridView = gridControlTableStatus.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            gridView.ColumnPanelRowHeight= (gridControlTableStatus.Height-20) / 6;
            gridView.RowHeight = (gridControlTableStatus.Height - 20) / 6;

            gridControlTableDataPosProject.DataSource = TableManager.tableData.TableSingleData.listTableDataPos;
            gridControlTableDataPosGlobal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataPos;
            gridControlTableDataSpdGloabal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataSpeed;
            gridControlTableDataToolGloabal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataTool;
            gridControlTableDataLocalGloabal.DataSource = TableManager.globaltableData.TableSingleData.listTableDataLocal;


            GridView gridViewProject = gridControlTableDataPosProject.MainView as GridView;
            AddNewColForProject(gridViewProject, "Get");
            AddNewColForProject(gridViewProject, "Jump");
            AddNewColForProject(gridViewProject, "Move");
            AddNewColForProject(gridViewProject, "Go");
            AddNewColForProject(gridViewProject, "Stop");

            GridView gridViewGlobal= gridControlTableDataPosGlobal.MainView as GridView;
            AddNewColForGlobal(gridViewGlobal, "Get");
            AddNewColForGlobal(gridViewGlobal, "Jump");
            AddNewColForGlobal(gridViewGlobal, "Move");
            AddNewColForGlobal(gridViewGlobal, "Go");
            AddNewColForGlobal(gridViewGlobal, "Stop");

            timerStatusScan.Enabled = true;
        }
        public void AddNewColForProject(GridView gridView, string strColName)
        {
            RepositoryItemButtonEdit repositoryItemButton = new RepositoryItemButtonEdit();
            repositoryItemButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            repositoryItemButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            repositoryItemButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;

            repositoryItemButton.Buttons[0].Caption = strColName;
            repositoryItemButton.TextEditStyle = TextEditStyles.HideTextEditor;
            gridView.Columns.AddVisible(strColName);

            gridView.Columns[strColName].ColumnEdit = repositoryItemButton;

            repositoryItemButton.Buttons[0].Click += ProjectGridCtrolButton_Click; ;
        }

        private void ProjectGridCtrolButton_Click(object sender, EventArgs e)
        {
            EditorButton editorButton = sender as EditorButton;
            GridView gridView = gridControlTableDataPosProject.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                if (editorButton.Caption == "Get")
                {
                    if (XtraMessageBox.Show("是真的要更新位置--" + TableManager.tableActioner.tableDate.listTableDataPos[iIndex].strName, "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    {
                        return;
                    }
                    TableManager.tableActioner.tableDate.listTableDataPos[iIndex].dXPos = axisStatuses[0].CrdPos;
                    TableManager.tableActioner.tableDate.listTableDataPos[iIndex].dYPos = axisStatuses[1].CrdPos;
                    TableManager.tableActioner.tableDate.listTableDataPos[iIndex].dZPos = axisStatuses[2].CrdPos;
                    TableManager.tableActioner.tableDate.listTableDataPos[iIndex].dUPos = axisStatuses[3].CrdPos;
                    gridControlTableDataPosProject.RefreshDataSource();
                }
                if (editorButton.Caption == "Jump")
                {
                    JumpPosition(iIndex,false);
                }
                if (editorButton.Caption == "Move")
                {
                    MovePosition(iIndex,false);
                }
                if (editorButton.Caption == "Go")
                {
                    GoPosition(iIndex,false);
                }
                if (editorButton.Caption == "Stop")
                {
                    TableManager.tableActioner.StopALL();
                }
                XtraMessageBox.Show("Row:" + iIndex.ToString() + "Click--" + "Action:" + editorButton.Caption);
            }


        }
        private void GoPosition(int iPos,bool bGlobal)
        {
            TableManager.tableActioner.tableMotionSpeed.dStartSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
            TableManager.tableActioner.tableMotionSpeed.dMoveSpeed = zoomTrackBarControlSpd.Value;
            TableManager.tableActioner.tableMotionSpeed.dEndSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
            TableManager.tableActioner.SetSpd(TableManager.tableActioner.tableMotionSpeed);
            if (bGlobal)
                TableManager.tableActioner.Go(TableManager.tableActioner.tableDateGlabal.listTableDataPos[iPos]);
            else
                TableManager.tableActioner.Go(TableManager.tableActioner.tableDate.listTableDataPos[iPos]);
        }
        private void MovePosition(int iPos, bool bGlobal)
        {
            TableManager.tableActioner.tableMotionSpeed.dStartSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
            TableManager.tableActioner.tableMotionSpeed.dMoveSpeed = zoomTrackBarControlSpd.Value;
            TableManager.tableActioner.tableMotionSpeed.dEndSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
            TableManager.tableActioner.SetSpd(TableManager.tableActioner.tableMotionSpeed);
            if (bGlobal)
                TableManager.tableActioner.Move(TableManager.tableActioner.tableDateGlabal.listTableDataPos[iPos]);
            else
                TableManager.tableActioner.Move(TableManager.tableActioner.tableDate.listTableDataPos[iPos]);
        }
        private void JumpPosition(int iPos, bool bGlobal)
        {
            TableManager.tableActioner.tableMotionSpeed.dStartSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
            TableManager.tableActioner.tableMotionSpeed.dMoveSpeed = zoomTrackBarControlSpd.Value;
            TableManager.tableActioner.tableMotionSpeed.dEndSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
            TableManager.tableActioner.SetSpd(TableManager.tableActioner.tableMotionSpeed);
            if (bGlobal)
                TableManager.tableActioner.Jump(TableManager.tableActioner.tableDateGlabal.listTableDataPos[iPos]);
            else
                TableManager.tableActioner.Jump(TableManager.tableActioner.tableDate.listTableDataPos[iPos]);
        }
        public void AddNewColForGlobal(GridView gridView, string strColName)
        {
            RepositoryItemButtonEdit repositoryItemButton = new RepositoryItemButtonEdit();
            repositoryItemButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            repositoryItemButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            repositoryItemButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;

            repositoryItemButton.Buttons[0].Caption = strColName;
            repositoryItemButton.TextEditStyle = TextEditStyles.HideTextEditor;
            gridView.Columns.AddVisible(strColName);

            gridView.Columns[strColName].ColumnEdit = repositoryItemButton;

            repositoryItemButton.Buttons[0].Click += GlobalGridCtrolButton_Click; ;
        }

        private void GlobalGridCtrolButton_Click(object sender, EventArgs e)
        {
            EditorButton editorButton = sender as EditorButton;
            GridView gridView = gridControlTableDataPosGlobal.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                if (editorButton.Caption == "Get")
                {
                    if (XtraMessageBox.Show("是真的要更新位置--" + TableManager.tableActioner.tableDateGlabal.listTableDataPos[iIndex].strName, "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
                    {
                        return;
                    }
                    TableManager.tableActioner.tableDateGlabal.listTableDataPos[iIndex].dXPos = axisStatuses[0].CrdPos;
                    TableManager.tableActioner.tableDateGlabal.listTableDataPos[iIndex].dYPos = axisStatuses[1].CrdPos;
                    TableManager.tableActioner.tableDateGlabal.listTableDataPos[iIndex].dZPos = axisStatuses[2].CrdPos;
                    TableManager.tableActioner.tableDateGlabal.listTableDataPos[iIndex].dUPos = axisStatuses[3].CrdPos;
                    gridControlTableDataPosGlobal.RefreshDataSource();
                }
                if (editorButton.Caption == "Jump")
                {
                    JumpPosition(iIndex, true);
                }
                if (editorButton.Caption == "Move")
                {
                    MovePosition(iIndex, true);
                }
                if (editorButton.Caption == "Go")
                {
                    GoPosition(iIndex, true);
                }
                if (editorButton.Caption == "Stop")
                {
                    TableManager.tableActioner.StopALL();
                }
                //XtraMessageBox.Show("Row:" + iIndex.ToString() + "Click--" + "Action:" + editorButton.Caption);
            }


        }
        private void UpDateComBox()
        {
            
                //comboBoxTool.Items.Add(actioner.tableDate.listTableDataTool[0].strName);
                comboBoxEditTool.Properties.Items.Add("Default");
                foreach (TableDataTool itemTool in TableManager.globaltableData.TableSingleData.listTableDataTool)
                {
                    comboBoxEditTool.Properties.Items.Add(itemTool.strName);
                }
                comboBoxEditLocal.Properties.Items.Add("Default");
                foreach (TableDataLocal itemLocal in TableManager.globaltableData.TableSingleData.listTableDataLocal)
                {
                    comboBoxEditLocal.Properties.Items.Add(itemLocal.strName);
                }

                comboBoxEditTool.Text = TableManager.tableActioner.toolSetting.strName;
                comboBoxEditLocal.Text = TableManager.tableActioner.localSetting.strName;
            
        }
        private void timerStatusScan_Tick(object sender, EventArgs e)
        {
            TableDataPosition posCurrent = TableManager.tableActioner.WhereTable();
            TableDataPosition posAct = TableManager.tableActioner.WhereAtToolAndLocal();
            double iConveryerCmdPos = 0;
            if (FrameManager.MachineStatus.Is4Axis)
            {
                FrameManager.FrameManager.MotionCard.GetCmdPos(4, out iConveryerCmdPos);
            }
            else
            {
                FrameManager.FrameManager.MotionCard.GetCmdPos(3, out iConveryerCmdPos);
            }

            axisStatuses[0].CmdPos = TableManager.tableActioner.GetCommandAxis(AxisName.X);
            axisStatuses[1].CmdPos = TableManager.tableActioner.GetCommandAxis(AxisName.Y);
            axisStatuses[2].CmdPos = TableManager.tableActioner.GetCommandAxis(AxisName.Z);
            axisStatuses[3].CmdPos = TableManager.tableActioner.GetCommandAxis(AxisName.U);
            axisStatuses[4].CmdPos = iConveryerCmdPos;

            axisStatuses[0].ActPos = posCurrent.XPos;
            axisStatuses[1].ActPos = posCurrent.YPos;
            axisStatuses[2].ActPos = posCurrent.ZPos;
            axisStatuses[3].ActPos = posCurrent.UPos;
            axisStatuses[4].ActPos = iConveryerCmdPos;

            axisStatuses[0].CrdPos = posAct.XPos;
            axisStatuses[1].CrdPos = posAct.YPos;
            axisStatuses[2].CrdPos = posAct.ZPos;
            axisStatuses[3].CrdPos = posAct.UPos;
            axisStatuses[4].CrdPos = iConveryerCmdPos;

            axisStatuses[0].CCWL = TableManager.tableActioner.GetCCWL(AxisName.X);
            axisStatuses[1].CCWL = TableManager.tableActioner.GetCCWL(AxisName.Y);
            axisStatuses[2].CCWL = TableManager.tableActioner.GetCCWL(AxisName.Z);
            axisStatuses[3].CCWL = TableManager.tableActioner.GetCCWL(AxisName.U);

            axisStatuses[0].Home = TableManager.tableActioner.GetHome(AxisName.X);
            axisStatuses[1].Home = TableManager.tableActioner.GetHome(AxisName.Y);
            axisStatuses[2].Home = TableManager.tableActioner.GetHome(AxisName.Z);
            axisStatuses[3].Home = TableManager.tableActioner.GetHome(AxisName.U);

            axisStatuses[0].CWL = TableManager.tableActioner.GetCWL(AxisName.X);
            axisStatuses[1].CWL = TableManager.tableActioner.GetCWL(AxisName.Y);
            axisStatuses[2].CWL = TableManager.tableActioner.GetCWL(AxisName.Z);
            axisStatuses[3].CWL = TableManager.tableActioner.GetCWL(AxisName.U);

            axisStatuses[0].Alarm = TableManager.tableActioner.GetAlarm(AxisName.X);
            axisStatuses[1].Alarm = TableManager.tableActioner.GetAlarm(AxisName.Y);
            axisStatuses[2].Alarm = TableManager.tableActioner.GetAlarm(AxisName.Z);
            axisStatuses[3].Alarm = TableManager.tableActioner.GetAlarm(AxisName.U);

            axisStatuses[0].ServoOn = TableManager.tableActioner.GetServoOn(AxisName.X);
            axisStatuses[1].ServoOn = TableManager.tableActioner.GetServoOn(AxisName.Y);
            axisStatuses[2].ServoOn = TableManager.tableActioner.GetServoOn(AxisName.Z);
            axisStatuses[3].ServoOn = TableManager.tableActioner.GetServoOn(AxisName.U);

            axisStatuses[0].MoveDone = TableManager.tableActioner.MoveDone(AxisName.X);
            axisStatuses[1].MoveDone = TableManager.tableActioner.MoveDone(AxisName.Y);
            axisStatuses[2].MoveDone = TableManager.tableActioner.MoveDone(AxisName.Z);
            axisStatuses[3].MoveDone = TableManager.tableActioner.MoveDone(AxisName.U);

            gridControlTableStatus.RefreshDataSource();
        }

        private void comboBoxEditTool_SelectedValueChanged(object sender, EventArgs e)
        {
            TableManager.tableActioner.SetTool(comboBoxEditTool.Text);
        }

        private void comboBoxEditLocal_SelectedValueChanged(object sender, EventArgs e)
        {
            TableManager.tableActioner.SetLocal(comboBoxEditLocal.Text);
        }

        private void simpleButtonCWY_MouseDown(object sender, MouseEventArgs e)
        {
            SimpleButton simpleButton= sender as SimpleButton;
            if (simpleButton != null)
            {
                TableManager.tableActioner.tableMotionSpeed.dStartSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
                TableManager.tableActioner.tableMotionSpeed.dMoveSpeed = zoomTrackBarControlSpd.Value;
                TableManager.tableActioner.tableMotionSpeed.dEndSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
                TableManager.tableActioner.tableMotionSpeed.dAcc = TableManager.tableActioner.tableMotionSpeed.dMoveSpeed* TableManager.tableActioner.tableMotionSpeed.dMoveSpeed * 10;
                TableManager.tableActioner.tableMotionSpeed.dDec = TableManager.tableActioner.tableMotionSpeed.dMoveSpeed* TableManager.tableActioner.tableMotionSpeed.dMoveSpeed * 10;
                TableManager.tableActioner.SetSpd(TableManager.tableActioner.tableMotionSpeed);
                if (simpleButton.Text=="X+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.X, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.X, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "X-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.X, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.X, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Y+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Y, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Y, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Y-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Y, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Y, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Z+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Z, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Z, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Z-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Z, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Z, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "U+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.U, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.U, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "U-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.U, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.U, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
            }
        }

        private void simpleButtonCWU_MouseUp(object sender, MouseEventArgs e)
        {
            if (comboBoxEditMode.SelectedIndex == 0)
            {

                TableManager.tableActioner.StopALL();
            }
        }

        private void simpleButtonHomeU_Click(object sender, EventArgs e)
        {
            SimpleButton simpleButton = sender as SimpleButton;
            if (simpleButton != null)
            {
                if (XtraMessageBox.Show("是否回原点!", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    return;
                }
                TableManager.tableActioner.tableMotionSpeed.dStartSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
                TableManager.tableActioner.tableMotionSpeed.dMoveSpeed = zoomTrackBarControlSpd.Value;
                TableManager.tableActioner.tableMotionSpeed.dEndSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
                TableManager.tableActioner.SetSpd(TableManager.tableActioner.tableMotionSpeed);
                if (simpleButton.Text == "UHome")
                {
                    TableManager.tableActioner.Home(AxisName.U);
                }
                if (simpleButton.Text == "XHome")
                {
                    TableManager.tableActioner.Home(AxisName.X);
                }
                if (simpleButton.Text == "YHome")
                {
                    TableManager.tableActioner.Home(AxisName.Y);
                }
                if (simpleButton.Text == "ZHome")
                {
                    TableManager.tableActioner.Home(AxisName.Z);
                }
            }
        }
    }
}